#coding=UTF-8
import re
import datetime
import unittest
import appparser

class TestURLParser(unittest.TestCase):
  def setUp(self):
    self.parser = appparser.URLParser("http://cndata4u.appspot.com/index.html")
    self.assertEqual(self.parser.root, "http://cndata4u.appspot.com")
    self.assertNotEqual(self.parser, None)
    
  def test_handle_starttag(self):
    self.parser.reset()
    self.parser.handle_starttag("a", (("href", "http://cndata4u.appspot.com/index.html"),))
    self.assertEqual(len(self.parser.urls), 1)
    
  def test_join(self):
    self.parser.reset()
    
    input = [("/test.jpg", ""),
             ("http://cndata4u.appspot.com/test1.htm", "http://cndata4u.appspot.com/test1.htm"),
             ("/test2.htm", "http://cndata4u.appspot.com/test2.htm"),
             ("/test3.html", "http://cndata4u.appspot.com/test3.html"),
             ("test4.html", "http://cndata4u.appspot.com/test4.html"),
             ]
    for i in input:
      self.assertEqual(self.parser.join(i[0]), i[1])

class TestTableParser(unittest.TestCase):
  def setUp(self):
    self.parser = appparser.TableParser()
    self.assertNotEqual(self.parser, None)
    
  def test_handle_entityref(self):
    self.parser.reset()
    self.parser.tagStack = ["table", "tr", "td", "span"]
    self.parser.handle_entityref("nbsp")
    self.assertEqual(self.parser.dataIndent, 1)
    self.parser.data = "hello"
    self.parser.handle_entityref("nbsp")
    self.assertEqual(self.parser.dataIndent, 1)
    
  def test_handle_data(self):
    self.parser.tagStack = ["table", "tr", "td"]
    data = "   \n\t"
    self.parser.handle_data(data)
    self.assertEqual(self.parser.data, "")
    
    data = "world"
    self.parser.data = "hello"
    self.parser.handle_data(data)
    self.assertEqual(self.parser.data, "helloworld")
    
    self.parser.tagStack = ["head", "style"]
    data = ".xl123{mso-char-indent-count:1;}"
    self.parser.handle_data(data)
    self.assertEqual(self.parser.style, {"xl123":1})
  
  def test_handle_starttag(self):
    self.parser.tagStack.append("something")
    self.assertFalse(len(self.parser.tableStack))
    self.parser.handle_starttag("table", None)
    self.assertTrue(len(self.parser.tableStack))
    self.assertEqual(len(self.parser.tagStack), 1)
    
    self.parser.reset()
    self.parser.table.currCell.col = 10
    self.parser.handle_starttag("tr", None)
    self.assertEqual(self.parser.table.currCell.col, 0)

    self.parser.reset()
    self.parser.handle_starttag("td", (("colspan", 1),("rowspan", 2)))
    self.assertEqual(self.parser.table.currCell.colSpan, 1)
    self.assertEqual(self.parser.table.currCell.rowSpan, 2)
    self.assertEqual(self.parser.table.cells[self.parser.table.currCell.row]
                     [self.parser.table.currCell.col], "Single")    
    self.assertEqual(self.parser.table.cells[self.parser.table.currCell.row+1]
                     [self.parser.table.currCell.col], "Merged")
    
    self.parser.reset()
    self.parser.style["xl123"] = 100
    self.parser.handle_starttag("th", (("colspan", 1),("rowspan", 2),("class", "xl123")))
    self.assertEqual(self.parser.table.currCell.colSpan, 1)
    self.assertEqual(self.parser.table.currCell.rowSpan, 2)
    self.assertEqual(self.parser.table.cells[self.parser.table.currCell.row]
                     [self.parser.table.currCell.col], "Single")    
    self.assertEqual(self.parser.table.cells[self.parser.table.currCell.row+1]
                     [self.parser.table.currCell.col], "Merged")
    self.assertEqual(self.parser.dataIndent, 99)
  
  def test_handle_endtag(self):
    self.assertFalse(len(self.parser.tagStack))
    self.parser.tagStack.append("something")
    self.parser.handle_endtag("body")
    self.assertEqual(self.parser.tagStack[-1], "something")
    
    self.parser.reset()
    self.parser.tagStack = ["table", "tr", "td", "table"]
    self.parser.tableStack = [(["table", "tr", "td"],
                               appparser.TagLevel1(),
                               appparser.Table(),
                               appparser.Units(),
                               appparser.Tags())]
    self.parser.data = "something"
    self.parser.handle_endtag("table")
    self.assertEqual(len(self.parser.tagStack), 3)
    self.assertFalse(len(self.parser.tableStack))
    self.assertTrue(self.parser.data)
    
    self.parser.reset()
    self.parser.tagStack = ["table", "tr", "td"]
    self.parser.data = "2009.01"
    self.parser.table.currCell.col = 1
    self.parser.table.currCell.row = 1
    self.parser.table.cells[1] = {1:"Single"}
    self.parser.handle_endtag("td")
    self.assertEqual(self.parser.dates.date[0].strftime("%Y%m%d"), "20090101")
    self.assertFalse(self.parser.data)
    self.assertEqual(self.parser.table.currCell.col, 2)
    
    self.parser.reset()
    self.parser.tagStack = ["table", "tr", "th"]
    self.parser.data = "2009.01"
    self.parser.table.currCell.col = 1
    self.parser.table.currCell.row = 1
    self.parser.table.cells[1] = {1:"Single"}
    self.parser.handle_endtag("th")
    self.assertEqual(self.parser.dates.date[0].strftime("%Y%m%d"), "20090101")
    self.assertFalse(self.parser.data)
    self.assertEqual(self.parser.table.currCell.col, 2)

    self.parser.reset()
    self.parser.tagStack = ["table", "tr", ]
    self.parser.table.currCell.row = 1
    self.parser.handle_endtag("tr")
    self.assertFalse(self.parser.data)
    self.assertEqual(self.parser.table.currCell.row, 2)
    
  def test_filterCell(self):
    self.parser.filterCell("2009.01")
    self.assertEqual(self.parser.dates.date[0].strftime("%Y%m%d"), "20090101")
    
    self.parser.filterCell(u"单位：亿人民币")
    self.assertEqual(self.parser.units.unit, u"亿人民币")

    self.parser.filterCell(u"123.4元")
    self.assertTrue(len(self.parser.records))

    self.parser.reset()
    self.parser.filterCell(u"一、存款")
    self.assertTrue(self.parser.tagLevel)
    self.assertEqual(len(self.parser.tags.colTags), 1)
    self.assertEqual(len(self.parser.tags.rowTags), 1)
    
    self.parser.reset()
    self.parser.filterCell(u"A股")
    self.assertEqual(len(self.parser.tags.colTags), 1)
    self.assertEqual(len(self.parser.tags.rowTags), 1)


  def test_filterRecord(self):
    self.parser.units.unit = "RMB"
    input = [("1863", "1863", "RMB"),
             ("494719.07", "494719.07", "RMB"),
             ("-83557.49", "-83557.49", "RMB"),
             ("something", "something", ""),
             (u"+1234.5元", "+1234.5", u"元"),
             (u"-1234.5元", "-1234.5", u"元"),
             (u"元+1234.5", "+1234.5", u"元"),
             (u"元-1234.5", "-1234.5", u"元"),
             (u"1天", u"1天", u""),
             (u"16个月", u"16个月", u""),
             (u"2年", u"2年", u""),
             (u"A股", u"A股", u""),
             ]
    for i in input:
      data, unit = self.parser.filterRecord(i[0])
      self.assertEqual(data, i[1])
      self.assertEqual(unit, i[2])
  
  def test_hasBlankDate(self):
    self.parser.reset()
    self.assertFalse(self.parser.hasBlankDate())
    self.parser.records.append(1)
    self.parser.dates.dateRow = 0
    self.parser.dates.colDates[0] = datetime.datetime.now()
    self.assertTrue(self.parser.hasBlankDate())
    self.parser.dates.colDates[0] = self.parser.dates.date
    self.assertFalse(self.parser.hasBlankDate())
    self.parser.dates.colDates[0] = datetime.datetime.now()
    self.parser.dates.colDates[1] = datetime.datetime.now()
    self.assertTrue(self.parser.hasBlankDate())
    self.parser.dates.colDateHits[0] = True
    self.parser.dates.colDateHits[1] = True
    self.assertFalse(self.parser.hasBlankDate())
    
    self.parser.reset()
    self.parser.records.append(1)
    self.parser.dates.dateCol = 0
    self.parser.dates.rowDates[0] = datetime.datetime.now()
    self.assertTrue(self.parser.hasBlankDate())
    self.parser.dates.rowDates[0] = self.parser.dates.date
    self.assertFalse(self.parser.hasBlankDate())
    self.parser.dates.rowDates[0] = datetime.datetime.now()
    self.parser.dates.rowDates[1] = datetime.datetime.now()
    self.assertTrue(self.parser.hasBlankDate())
    self.parser.dates.rowDateHits[0] = True
    self.parser.dates.rowDateHits[1] = True
    self.assertFalse(self.parser.hasBlankDate())

  def test_saveStyleInfo(self):
    self.parser.reset()
    data = ".xl123{mso-char-indent-count:1;}"
    self.parser.saveStyleInfo(data)
    self.assertEqual(self.parser.style, {"xl123":1})
    
  def test_setDataIndent(self):
    self.parser.reset()
    attrs = (("class", "abc123"), ("style", "others"))
    self.parser.style["abc123"] = 100
    self.parser.setDataIndent(attrs)
    self.assertEqual(self.parser.dataIndent, 99)
    
  def test_hasTagLevel(self):
    self.parser.reset()
    self.assertTrue(self.parser.hasTagLevel())
    self.parser.dates.dateCol = 0
    self.assertFalse(self.parser.hasTagLevel())
    self.parser.reset()
    self.parser.dates.dateRow = 3
    self.parser.table.currCell.row = 3
    self.assertFalse(self.parser.hasTagLevel())
  
  def test_table_setCellInfo(self):
    self.parser.reset()
    attrs = (("colspan", 2),("rowspan", 3))
    self.parser.table.setCellInfo(attrs)
    self.assertEqual(self.parser.table.currCell.colSpan, 2)
    self.assertEqual(self.parser.table.currCell.rowSpan, 3)
    self.assertEqual(self.parser.table.cells[self.parser.table.currCell.row]
                     [self.parser.table.currCell.col], "Single")
    self.assertEqual(self.parser.table.currCell.col, 0)
    
    self.parser.table.currCell.col += 1
    self.parser.table.setCellInfo(())
    self.assertEqual(self.parser.table.currCell.colSpan, 1)
    self.assertEqual(self.parser.table.currCell.rowSpan, 1)
    self.assertEqual(self.parser.table.currCell.col, 2)
  
  def test_table_fillCell(self):
    self.parser.reset()
    self.parser.table.cells[0] = {0:"Merged"}
    self.assertEqual(self.parser.table.fillCell(), "Merged")
    
    self.parser.reset()
    self.parser.table.currCell.colSpan = 2
    self.parser.table.currCell.rowSpan = 3
    self.assertEqual(self.parser.table.fillCell(), "Single")
    i = 0
    while i<3:
      j = 0
      while j<2:
        result = "Single" if 0 == i and 0 == j else "Merged" 
        self.assertEqual(self.parser.table.cells[i][j], result)
        j+=1
      i+=1
  
  def test_table_isSingleCell(self):
    self.parser.reset()
    self.parser.table.cells[0] = {0:"Single"}
    self.assertTrue(self.parser.table.isSingleCell())
    
  def test_table_incCurrCol(self):
    self.parser.reset()
    self.parser.table.incCurrCol(2)
    self.assertEqual(self.parser.table.currCell.col, 2)

  def test_table_incCurrRow(self):
    self.parser.reset()
    self.parser.table.incCurrRow(2)
    self.assertEqual(self.parser.table.currCell.row, 2)
  
  def test_dates_hasColDate(self):
    self.parser.reset()
    self.assertFalse(self.parser.dates.hasColDate())
    self.parser.dates.dateCol = 0
    self.assertTrue(self.parser.dates.hasColDate())
    
  def test_dates_hasRowDate(self):
    self.parser.reset()
    self.assertFalse(self.parser.dates.hasRowDate())
    self.parser.dates.dateRow = 0
    self.assertTrue(self.parser.dates.hasRowDate())
    
  def test_dates_getDate(self):
    self.parser.reset()
    self.assertEqual(self.parser.dates.getDate(self.parser.table.currCell),
                     self.parser.dates.date)
    self.assertFalse(len(self.parser.dates.colDateHits))
    self.assertFalse(len(self.parser.dates.rowDateHits))
    self.parser.dates.dateCol = 0
    self.parser.dates.rowDates[0] = datetime.datetime.strptime("20090421", "%Y%m%d")
    self.assertEqual(self.parser.dates.getDate(
                    self.parser.table.currCell).strftime("%Y%m%d"), "20090421")
    self.assertTrue(len(self.parser.dates.rowDateHits))
    self.parser.dates.dateCol = -1
    self.parser.dates.dateRow = 0
    self.parser.dates.colDates[0] = datetime.datetime.strptime("20090421", "%Y%m%d")
    self.assertEqual(self.parser.dates.getDate(
                    self.parser.table.currCell).strftime("%Y%m%d"), "20090421")
    self.assertTrue(len(self.parser.dates.colDateHits))
    
  def test_dates_filterDate(self):
    self.parser.reset()
    self.parser.table.currCell.col = 1
    self.parser.table.currCell.row = 2
    self.assertFalse(self.parser.dates.filterDate(self.parser.table.currCell, "2009.04.21"))
    self.assertEqual(self.parser.dates.date[0].strftime(self.parser.dates.date[1]), "20090421")
    self.assertEqual(self.parser.dates.dateCol, 1)
    self.assertEqual(self.parser.dates.dateRow, 2)
    self.assertEqual(self.parser.dates.colDates[1][0].strftime("%Y%m%d"), "20090421")
    self.assertEqual(self.parser.dates.rowDates[2][0].strftime("%Y%m%d"), "20090421")
    
    self.parser.table.currCell.col = 1
    self.parser.table.currCell.row = 3
    self.assertFalse(self.parser.dates.filterDate(self.parser.table.currCell, "2009.04.22"))
    self.assertEqual(self.parser.dates.date[0].strftime(self.parser.dates.date[1]), "20090421")
    self.assertEqual(self.parser.dates.dateCol, 1)
    self.assertEqual(self.parser.dates.dateRow, -1)
    self.assertEqual(self.parser.dates.rowDates[3][0].strftime("%Y%m%d"), "20090422")

    self.parser.dates.dateCol = 1
    self.parser.dates.dateRow = 2
    self.parser.table.currCell.col = 2
    self.parser.table.currCell.row = 2
    self.assertFalse(self.parser.dates.filterDate(self.parser.table.currCell, "2009.04.23"))
    self.assertEqual(self.parser.dates.date[0].strftime(self.parser.dates.date[1]), "20090421")
    self.assertEqual(self.parser.dates.dateCol, -1)
    self.assertEqual(self.parser.dates.dateRow, 2)
    self.assertEqual(self.parser.dates.colDates[2][0].strftime("%Y%m%d"), "20090423")
    
  def test_dates_updateGlobalDate(self):
    self.parser.reset()
    self.parser.dates.dateCol = 0
    self.parser.dates.rowDates[0] = (datetime.datetime.strptime("20090421", "%Y%m%d"), "%Y%m%d")
    self.parser.dates.updateGlobalDate(1)
    self.assertEqual("20090421", self.parser.dates.date[0].strftime("%Y%m%d"))
    
  def test_IsDate(self):
    input = [(u"一季度", "20090331", "%Y%m%d"),
             (u"二季度", "20090630", "%Y%m%d"),
             (u"三季度", "20090930", "%Y%m%d"),
             (u"四季度", "20091231", "%Y%m%d"),
             (u"Something2009年4月21日", "20090421", "%Y%m%d"),
             (u"Something2009年4月21号", "20090421", "%Y%m%d"),
             ("Something2009.4.21", "20090421", "%Y%m%d"),
             ("Something2009-4-21", "20090421", "%Y%m%d"),
             (u"Something2009年4月", "20090401", "%Y%m"),
             ("Something2009.04", "20090401", "%Y%m"),
             ("Something2009-04", "20090401", "%Y%m"),
             (u"Something2009年", "20091231", "%Y"),
             ("Something2009", "20091231", "%Y"),
             (u"2009年4月21日", "20090421", "%Y%m%d"),
             (u"2009年4月21号", "20090421", "%Y%m%d"),
             ("2009.4.21", "20090421", "%Y%m%d"),
             ("2009-4-21", "20090421", "%Y%m%d"),
             (u"2009年4月", "20090401", "%Y%m"),
             ("2009.04", "20090401", "%Y%m"),
             ("2009-04", "20090401", "%Y%m"),
             (u"2009年", "20091231", "%Y"),
             ("2009", "20091231", "%Y"),
             (u"Something4月21日", "20090421", "%Y%m%d"),
             (u"Something4月21号", "20090421", "%Y%m%d"),
             (u"Something4月", "20090401", "%Y%m"),
             (u"4月21日", "20090421", "%Y%m%d"),
             (u"4月21号", "20090421", "%Y%m%d"),
             (u"4月", "20090401", "%Y%m"),
             (u"Something21日", "20090121", "%Y%m%d"),
             (u"Something21号", "20090121", "%Y%m%d"),
             (u"21日", "20090121", "%Y%m%d"),
             (u"21号", "20090121", "%Y%m%d"),
             ]
    for i in input:
      matched, date, format = appparser.IsDate(i[0], datetime.datetime.strptime("20090101", "%Y%m%d"))
      self.assertTrue(matched)      
      self.assertEqual(date.strftime("%Y%m%d"), i[1])
      self.assertEqual(format, i[2])
      
  def test__replNormal(self):
    fmt = {1:"%Y%m%d"}
    ymd = [("2009.04.21"),
           ("20090421")]
    date, format = appparser._replNormal("2009.04.21", "", ymd, fmt)
    self.assertEqual(date.strftime("%Y%m%d"), "20090421")
    self.assertEqual(format, fmt[1])
  
  def test__replQuarter(self):
    fmt = {1:"%Y"}
    input = [(u"一", "20090331"),
             (u"二", "20090630"),
             (u"三", "20090930"),
             (u"四", "20091231"),
             ]
    
    for i in input:
      date, format = appparser._replQuarter(i[0], "2009", "", fmt)
    self.assertEqual(date.strftime("%Y%m%d"), i[1])
    self.assertEqual(format, "%Y%m%d")

  def test__IsDate(self):
    matched, date, format = appparser._IsDate([("(?P<date>(2009))", appparser._replNormal)], "2009", "", {1:"%Y"})
    self.assertTrue(matched)
    self.assertEqual(date.strftime("%Y%m%d"), "20091231")
    self.assertEqual(format, "%Y")
        
  def test_unit_getUnit(self):
    self.parser.reset()
    self.parser.units.unit = "GlobalUnit"
    self.assertEqual(self.parser.units.getUnit(self.parser.table.currCell,
                                               self.parser.dates),
                     self.parser.units.unit)
    
    self.parser.units.colUnits[0] = "colUnit"
    self.parser.units.rowUnits[0] = "rowUnit"
    self.parser.dates.dateCol = 0
    self.assertEqual(self.parser.units.getUnit(self.parser.table.currCell,
                                               self.parser.dates), "colUnit")
    self.parser.dates.dateCol = -1
    self.parser.dates.dateRow = 0
    self.assertEqual(self.parser.units.getUnit(self.parser.table.currCell,
                                               self.parser.dates), "rowUnit")
    
    self.parser.dates.dateCol = -1
    self.parser.dates.dateRow = -1
    self.assertEqual(self.parser.units.getUnit(self.parser.table.currCell,
                                               self.parser.dates), "colUnit")
    
    self.parser.units.colUnits = {}
    self.parser.units.rowUnits[0] = "rowUnit"
    self.assertEqual(self.parser.units.getUnit(self.parser.table.currCell,
                                               self.parser.dates), "rowUnit")
    
    self.parser.units.unit = ""
    self.parser.units.colUnits[0] = "colUnit"
    self.parser.units.rowUnits = {}
    self.parser.table.currCell.col = 1
    self.assertEqual(self.parser.units.getUnit(self.parser.table.currCell,
                                               self.parser.dates), "colUnit")
    
    self.parser.units.unit = ""
    self.parser.units.colUnits = {}
    self.parser.units.rowUnits[0] = "rowUnit"
    self.parser.table.currCell.row = 1
    self.assertEqual(self.parser.units.getUnit(self.parser.table.currCell,
                                               self.parser.dates), "rowUnit")
    
  def test_unit_filterUnit(self):
    self.parser.reset()
    input = [(u"单位:亿人民币", u"", u""),
             (u"单位：亿人民币", u"", u""),
             (u"(人民币／美元）", u"", u"人民币／美元"),
             (u"(人民币／港币）", u"", u"人民币／港币"),
             (u"(元)", u"", u"元"),
             (u"(%)", u"", u"%"),
             (u"(\uff05)", u"", u"\uff05"),
             (u"(‰)", u"", u"‰"),    
             (u"(元宝)", u"(元宝)", u""),
             (u"(亿股)", u"", u"亿股"),
             (u"(按部门分类)", u"(按部门分类)", u""),
             (u"(上年=100)", u"", u"上年=100"),
             (u"系数(2006年=100)", u"系数", u"2006年=100"),
             (u"(千吨标准煤)", u"", u"千吨标准煤"),
             ("previouscorrespondingperiod=100", "previouscorrespondingperiod=100", ""),
             ]
    
    self.parser.units.unit = "something"
    self.assertEqual(self.parser.units.filterUnit(self.parser.table.currCell, u"以上年同月为100"), u"")
    self.assertEqual(self.parser.units.unit, "something")
    self.parser.units.unit = ""
    self.assertEqual(self.parser.units.filterUnit(self.parser.table.currCell, u"以上年同月为100"), u"")
    self.assertEqual(self.parser.units.unit, u"以上年同月为100")
    self.parser.units.unit = ""
    for i in input:
      self.parser.units.rowUnits[0] = ""
      self.parser.units.colUnits[0] = ""
      self.assertEqual(self.parser.units.filterUnit(self.parser.table.currCell, i[0]), i[1])
      self.assertEqual(self.parser.units.unit, u"亿人民币")
      self.assertEqual(self.parser.units.rowUnits[0], i[2])
      self.assertEqual(self.parser.units.colUnits[0], i[2])
      self.assertTrue(self.parser.units.hasUnit)
      
  def test_unit_replNormal(self):
    data = "(RMB)"
    m = re.match("\((?P<unit>RMB)\)", data)
    self.assertEqual(self.parser.units.replNormal(data, m), "")
    
  def test_unit_replGlobal(self):
    data = "(RMB)"
    m = re.match("\((?P<global>RMB)\)", data)
    self.assertEqual(self.parser.units.replGlobal(data, m), "") 
  
  def test_tags_getTag(self):
    self.parser.reset()
    self.parser.tags.colTags[0] = [("col1", (1, 0)), ("col2", (1, 0)), ("pub", (1, 0))]
    self.parser.tags.rowTags[0] = [("row1", (1, 0)), ("row2", (1, 0)), ("pub", (1, 0))]
    self.parser.tags.globalTags = ["global", "pub"]
    self.assertEqual(self.parser.tags.getTags(self.parser.table.currCell).sort(),
                     ["col1", "col2", "row1", "row2", "global", "pub"].sort())
    
  def test_tags_addTag(self):
    self.parser.reset()
    tags = {}
    self.parser.tags.addTag(tags, 1, u"", 3)
    self.assertEqual(tags, {})
    self.parser.tags.addTag(tags, 1, u"一", 3)
    self.assertEqual(tags, {1:[(u"一", (3, 1))]})
    self.parser.tags.addTag(tags, 1, u"一", 3)
    self.assertEqual(tags, {1:[(u"一", (3, 1))]})
    
    self.parser.tags.addTag(tags, 1, u"二", 1, True)
    self.assertEqual(tags, {1:[(u"一", (3, 1)),
                            (u"二", (1, 1))]})
    
    self.parser.tags.addTag(tags, 1, u"三", 1, True)
    self.assertEqual(tags, {1:[(u"一", (3, 1)),
                            (u"二三", (1, 1))]})
    
  def test_tags_addTags(self):
    self.parser.reset()
    self.parser.table.currCell.colSpan = 2
    self.parser.table.currCell.rowSpan = 2
    self.parser.tags.addTags(self.parser.table.currCell, u"标签")
    self.assertEqual(self.parser.tags.colTags, {0:[(u"标签", (2, 0))],
                                                1:[(u"标签", (2, 1))]})
    self.assertEqual(self.parser.tags.rowTags, {0:[(u"标签", (2, 0))],
                                                1:[(u"标签", (2, 1))]})
    
  def test_tags_refineTag(self):
    self.parser.reset()
    input = [(u"100-200米", u"100-200米"),
             (u"A股", u"A股"),
             (u"1天", u"1天"),
             (u"2个月", u"2个月"),
             (u"3年", u"3年"),
             (u"标签。", u"标签"),
             (u"标签≥", u"标签"),
             (u"标签≤", u"标签"),
             (u"本表标签", u""),
             (u"（净）", u""),
             (u"\n123-().#%=abcABC:：、％,／①②", ""),
             ]
    
    for i in input:
      self.assertEqual(self.parser.tags.refineTag(i[0]), i[1])
  
  def test_taglevel_check(self):
    self.parser.reset()
    taglevel = appparser.TagLevel()
    input = [(1, "hello", "hello", 0, False),
             (0, "hello", "hello", 99, False),
             (0, "1st", "1st", 0, False),
             (0, "2nd", "2nd", 1, False),
             ]
    for i in input:
      self.parser.table.currCell.col = i[0]
      data, matched = taglevel.check(self.parser, i[1], i[3])
      self.assertEqual(data, i[2])
      self.assertEqual(matched, i[4])
      
  def test_taglevel_getLevel(self):
    taglevel = appparser.TagLevel()
    data, level = taglevel.getLevel("hello", 10)
    self.assertEqual(data, "hello")
    self.assertEqual(level, 99)

  def test_taglevel_addTag(self):
    self.parser.reset()
    taglevel = appparser.TagLevel()
    input = [(0, u"第一级", 0, [(u"第一级", 0)], None),
             (1, u"第二级", 1, [(u"第一级", 0), (u"第二级", 1)], [(u"第一级", (1, 1))]),
             (2, u"第三级", 2, [(u"第一级", 0), (u"第二级", 1), (u"第三级", 2)], [(u"第一级", (1, 2)),(u"第二级", (1, 2))]),
             (3, u"第四级", 3, [(u"第一级", 0), (u"第二级", 1), (u"第三级", 2), (u"第四级", 3)], [(u"第一级", (1, 3)),(u"第二级", (1, 3)),(u"第三级", (1, 3))]),
             (4, u"第二二级", 1, [(u"第一级", 0), (u"第二二级", 1)], [(u"第一级", (1, 4))]),
             (5, u"第九九九级", 999, [(u"第一级", 0), (u"第二二级", 1),(u"第九九九级", 999)], [(u"第一级", (1, 5)),(u"第二二级", (1, 5)),(u"第九九九级", (1, 5)),]),
             ]
    for i in input:
      self.parser.table.currCell.row = i[0]
      taglevel.addTag(self.parser, i[1], i[2])
      self.assertEqual(taglevel.tagStack, i[3])
      if i[0]:
        self.assertEqual(self.parser.tags.rowTags[i[0]].sort(), i[4].sort())

  def test_taglevel1_getLevel(self):
    taglevel = appparser.TagLevel1()
    input = [("hello", "hello", 99),
             (u"一.第一级", u"第一级", 0),
             (u"A.第二级", u"第二级", 1),
             (u"b.第三级", u"第三级", 2),
             (u"1.第四级", u"第四级", 3),
             (u"1.1第五级", u"第五级", 4),
             (u"1.1.1第六级", u"第六级", 5),
             ]
    for i in input:
      data, level = taglevel.getLevel(i[0], 0)
      self.assertEqual(data, i[1])
      self.assertEqual(level, i[2])

  def test_taglevel2_check(self):
    self.parser.reset()
    taglevel = appparser.TagLevel2()
    input = [(1, "hello", "hello", 0, False, [("zero", 0), ("hello", 99)]),
             (0, "hello1", "hello1", 99, False, [("zero", 0), ("hello", 99), ("hello1", 99+99)]),
             (0, "1st", "1st", 0, False, [("zero", 0), ("1st", 99)]),
             (0, "2nd", "2nd", 1, False, [("zero", 0), ("1st", 99), ("2nd", 100)]),
             ]
    taglevel.tagStack.append(("zero", 0))
    for i in input:
      self.parser.table.currCell.col = i[0]
      data, matched = taglevel.check(self.parser, i[1], i[3])
      self.assertEqual(data, i[2])
      self.assertEqual(matched, i[4])
      self.assertEqual(taglevel.tagStack, i[5])
      
  def test_taglevel2_getLevel(self):
    taglevel = appparser.TagLevel2()
    input = [("hello", "hello", 99),
             (u"一、第一级", u"第一级", 0),
             (u"1.第二级", u"第二级", 1),
             (u"(1)第三级", u"第三级", 2),
             ]
    for i in input:
      data, level = taglevel.getLevel(i[0], 0)
      self.assertEqual(data, i[1])
      self.assertEqual(level, i[2])
      

    
    